﻿using DevComponents.DotNetBar.Controls;
using Public;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace BLE_Barcode
{
    public partial class robotstudio : DevComponents.DotNetBar.Office2007Form
    {
        string FileName = "默认示教.MKF";
        public robotstudio()
        {
            InitializeComponent();
        }
        private Thread threadPause;
        private void robotstudio_Load(object sender, EventArgs e)
        {

        }

        private void button104_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                this.Hide();
                Thread.Sleep(50);
            }

            CopyScream cp = new CopyScream();
            cp.ShowDialog();
            textBox1.Text = MoseKeyboard.x.ToString();
            textBox2.Text = MoseKeyboard.y.ToString();
            if (checkBox1.Checked == true)
                this.Show();
        }

        private void button106_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == null || textBox3.Text == "")
            {
                MessageBox.Show("位置和操作间隔不能为空");
                return;
            }
            listBox1.Items.Add(textBox3.Text.ToString() + ",Mouse," + textBox1.Text.ToString() + "," + textBox2.Text.ToString() + ",LeftButtonClick");
        }

        private void button107_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == null || textBox3.Text == "")
            {
                MessageBox.Show("位置和操作间隔不能为空");
                return;
            }
            listBox1.Items.Add(textBox3.Text.ToString() + ",Mouse," + textBox1.Text.ToString() + "," + textBox2.Text.ToString() + ",LeftButtonDoubleClick");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string type2 = "SN";          
            string key = "BARCODE" + numericUpDown1.Value + ";" + type2;
            AddKey(key);//增加按键操作
        }


        private void AddKey(string key)//增加按键操作
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("操作间隔不能为空");
                return;
            }
            string add = "";

            add += key;
            listBox1.Items.Add(textBox3.Text.ToString() + ",KeyBord," + add);
        }

        private void button103_Click(object sender, EventArgs e)
        {
            testbutton();
        }

        public void testbutton()
        {
            //if (dataGridView1.RowCount <= 0)
            //{
            //    MessageBox.Show("请扫描条码获取数据后再测试");
            //    return;
            //}
            //if (string.IsNullOrEmpty(linkLabel3.Text))
            //{
            //    MessageBox.Show("请选择执行脚本");
            //    return;
            //}
            this.Hide();
            Thread.Sleep(800);
            WriteLogTools.WriteMessage("------------------RunTest-----------------------");
            threadPause = new Thread(RunTest);
            threadPause.Start();
            this.Show();
        }

        public void RunTest()
        {
            //string filename = "result.txt";
            //测试按钮，测试当前列表中的命令是否可以成功执行
            string[] result = new string[listBox1.Items.Count];
            try
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    if (MoseKeyboard.ExcuteCmd(listBox1.Items[i].ToString()) == true)
                    {
                        result[i] = "执行命令成功：" + listBox1.Items[i].ToString();
                        //StatusLabel.Text = result[i];
                        WriteLogTools.WriteMessage(result[i]);
                    }
                    else
                    {
                        result[i] = "执行命令失败：" + listBox1.Items[i].ToString();
                        WriteLogTools.WriteMessage(result[i]);
                    }

                    //if (MoseKeyboard.GetState(KeyBord.NumLock) == true)//判断numLock
                    //    label1.ForeColor = Color.Green;
                    //else
                    //    label1.ForeColor = Color.DarkGray;
                    //if (MoseKeyboard.GetState(KeyBord.CapsLock) == true)
                    //    label2.ForeColor = Color.Green;
                    //else
                    //    label2.ForeColor = Color.DarkGray;
                    //if (MoseKeyboard.GetState(KeyBord.Scroll) == true)
                    //    label3.ForeColor = Color.Green;
                    //else
                    //    label3.ForeColor = Color.DarkGray;
                }
                //File.WriteAllLines(filename, result);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //System.Diagnostics.Process.Start(filename);//执行成功打开记事本
        }

        private void button99_Click(object sender, EventArgs e)
        {
            AddKey("Enter");
        }

        private void button109_Click(object sender, EventArgs e)
        {
            string filePath = GetDefaultFilePath();
            // 使用StreamWriter写入内容，并指定GB2312编码
            using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312")))
            {
                try
                {
                    WriteLogTools.WriteMessage("保存脚本：" + filePath); // 确保WriteLogTools类和WriteMessage方法存在
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        sw.WriteLine(listBox1.Items[i].ToString());
                    }
                    // 如果WriteMessage方法需要确认文件写入成功后再执行，则这里的日志写入应该在try块外部或适当的位置
                    // 注意：上面的WriteMessage调用已经放在了try块内部，如果它依赖于文件写入的结果，则可能需要调整
                    MessageBox.Show("保存成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "保存文件时出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }
        bool saveFlag = false;

        // 获取应用程序的相对目录下的“默认示教.MKF”文件路径
        private string GetDefaultFilePath()
        {
            string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
            return Path.Combine(appDirectory, "默认示教.MKF");
        }



        private void button111_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count != 0)
            {
                if (saveFlag == false)
                    if (MessageBox.Show("当前配置未保存，是否保存？", "注意", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        button109_Click(null, null);
                        saveFlag = true;
                        return;
                    }
            }
            //OpenFileDialog open = new OpenFileDialog();
            //open.Filter = " (*.MKF)|*.MKF";
            //open.FilterIndex = 0;
            //open.RestoreDirectory = true;
            //open.Title = "打开配置文件";
            //open.ShowDialog();
            //if (open.FileName == "")
            //    return;
          


            listBox1.Items.Clear();//清除配置
            try
            {
                WriteLogTools.WriteMessage("加载脚本：" + FileName);
                //linkLabel3.Text = linkLabel2.Text = System.IO.Path.GetFileName(open.FileName);
                string[] str = File.ReadAllLines(FileName);
                for (int i = 0; i < str.Length; i++)
                    if (str[i] != "")
                        listBox1.Items.Add(str[i]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                
            }
        }

        private void button115_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
                MessageBox.Show("请选择要移动的位置");
            else
            {
                if (listBox1.SelectedIndex > 0)
                {
                    object temp = listBox1.Items[listBox1.SelectedIndex];
                    listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex - 1];
                    listBox1.Items[listBox1.SelectedIndex - 1] = temp;
                    listBox1.SelectedIndex--;
                }
            }
        }

        private void button114_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
                MessageBox.Show("请选择要移动的位置");
            else
            {
                if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
                {
                    object temp = listBox1.Items[listBox1.SelectedIndex];
                    listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex + 1];
                    listBox1.Items[listBox1.SelectedIndex + 1] = temp;
                    listBox1.SelectedIndex++;
                }
            }
        }

        private void button113_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if (index >= 0)
                listBox1.Items.RemoveAt(index);
        }

        private void button112_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
    }
}
